package com.whpost.api.controller.wxht;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.whpost.api.controller.BaseController;
import com.whpost.api.utils.ApiUtils;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.WxBaseServices;
import com.whpost.service.service.t.FileService;
import com.whpost.service.service.t.TCzyServices;
import com.whpost.service.tools.Url;
import com.whpost.service.wx.entiy.WxBaoming;
import com.whpost.service.wx.service.BaomingServices;
import com.whpost.service.wx.service.WxbmServices;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wxbm")
public class WxbmController extends BaseController {

    @Resource
    private WxbmServices wxbmServices;

    @Resource
    private FileService fileService;

    @Autowired
    public WxbmController(WxbmServices wxbmServices) {
        super();
        setPerCode("wxbm");
        setBaseServices(wxbmServices);
    }

    @RequestMapping(value ="/query_xls")
    public ResultInfo queryXls(@RequestBody Map<String, Object> map, HttpServletResponse response) throws IOException {
        String loginId = ApiUtils.getLoginId();

        // 获取原始数据
        ResultInfo<Map<String, Object>> resultInfo = wxbmServices.query(loginId, map);
        Map<String, Object> map1 = resultInfo.getData();
        ListInfo<WxBaoming> wxBaomingListInfoListInfo = (ListInfo<WxBaoming>) map1.get("cxjg");
        String wdz = (String) map1.get("wdz");

        // 1. 获取要导出的列表
        List<WxBaoming> exportList = wxBaomingListInfoListInfo.getListInfo();

        // 2. 遍历列表，处理需要转换的属性
        if (exportList != null && !exportList.isEmpty()) {
            for (WxBaoming baoming : exportList) {
                // 处理性别字段（xb）
                if ("1".equals(baoming.getXb())) {
                    baoming.setXb("男"); // 替换为中文文本
                } else if ("2".equals(baoming.getXb())) {
                    baoming.setXb("女");
                }

                // 处理审核结果字段（shjg）
                if ("0".equals(baoming.getShjg())) {
                    baoming.setShjg("待审核");
                } else if ("1".equals(baoming.getShjg())) {
                    baoming.setShjg("审核通过");
                } else if ("2".equals(baoming.getShjg())) {
                    baoming.setShjg("不通过");
                }

                if(!StringUtils.isEmpty(baoming.getSjhm())){
                    String a = null;
                    try {
                        a = Url.decrypt(baoming.getSjhm());
                        baoming.setSjhm(a);
                    } catch (Exception e) {
                        //throw new RuntimeException(e);
                    }
                }

                if(!StringUtils.isEmpty(baoming.getJjlxrSjhm())){
                    String a = null;
                    try {
                        a = Url.decrypt(baoming.getJjlxrSjhm());
                        baoming.setJjlxrSjhm(a);
                    } catch (Exception e) {
                       // throw new RuntimeException(e);
                    }

                }
            }
        }

        // 3. 处理后的列表写入Excel
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ExcelWriterBuilder writerBuilder = EasyExcel.write(outputStream, WxBaoming.class)
                .registerWriteHandler(new SimpleColumnWidthStyleStrategy(30));
        writerBuilder.sheet("Sheet1").doWrite(exportList); // 注意这里传入处理后的exportList

        // 后续的临时文件处理、上传逻辑不变...
        File tempFile = File.createTempFile("比赛名单", ".xlsx");
        try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) {
            outputStream.writeTo(fileOutputStream);
        }
        Long fileId = fileService.uploadFile(tempFile, loginId);

        if (null != fileId) {
            resultInfo = new ResultInfo(7, "数据保存成功，准备下载文件！", String.valueOf(fileId));
        } else {
            resultInfo = new ResultInfo(8, "数据保存成功，但是文件生成失败！无法下载！", null);
        }

        return resultInfo;
    }
}
